package com.hxxy.wyp.petsLover.common.rest.base.backend;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.Message;
import com.hxxy.wyp.petsLover.common.entities.MessageReply;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.MessageAnyVo;
import com.hxxy.wyp.petsLover.common.services.base.backend.MessageReplyService;
import com.hxxy.wyp.petsLover.common.services.base.backend.MessageService;
import com.hxxy.wyp.petsLover.security.SecurityContextFrontUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName MessageRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/30 13:22
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/message")
public class MessageRest extends RestBase {
    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageReplyService messageReplyService;

    @Autowired
    private SecurityContextFrontUser securityContextFrontUser;

    /**
     * 查询所有留言列表以及留言回复
     *
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin","super_admin"},mode = SaMode.OR)
    @SaCheckPermission(value = {"user_load","all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping("/listMessage")
    public Object listMessage() {
        List<Message> list = messageService.listMessageAny();
        return ajaxSuccess(list, "200");
    }


    //    @SaCheckLogin
//    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
//    @SaCheckPermission(value = {"user_update", "all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping(value = "/addMessage",method = RequestMethod.POST)
    public Object addMessage(@Validated @RequestBody Message message) {
        User userInfo = securityContextFrontUser.getUserInfo();
        message.setName(userInfo.getNickname());
        message.setMessageInfo(userInfo.getPhone().toString());
        message.setUserId(userInfo.getId());
        message.setMessageTime(LocalDateTime.now());
        boolean save = messageService.save(message);
        if (save) {
            return ajaxSuccess("200", "发表成功!请等待审核~");
        } else {
            return ajaxSuccess("500", "发表失败!请等待审核~");
        }
    }

    /**
     * 回复留言
     *
     * @param messageReply
     * @return
     */
    @RequestMapping("/replyMessage")
    public Object addMessage(@RequestBody MessageReply messageReply) {
        messageReply.setMessageReplyTime(LocalDateTime.now());
        boolean save = messageReplyService.save(messageReply);
        if (save) {
            return ajaxSuccess("200", "回复成功!");
        } else {
            return ajaxSuccess("500", "回复失败!");
        }
    }

    /**
     * 删除留言
     */
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin","super_admin"},mode = SaMode.OR)
    @SaCheckPermission(value = {"user_delete","all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping("/deleteOneMessage")
    public Object deleteOneMessage(@RequestParam("id") String id) {
        boolean b = messageService.removeById(id);
        if (b) {
            return ajaxSuccess("200", "删除成功!");
        } else {
            return ajaxSuccess("500", "删除失败!");
        }
    }

    /**
     * 审核留言
     *
     * @param id
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin","super_admin"},mode = SaMode.OR)
    @SaCheckPermission(value = {"user_auth","all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping("/authorMessage")
    public Object authorOneMessage(@RequestParam("id") String id) {
        Message message = messageService.getById(id);
        message.setStatus("1");
        boolean b = messageService.updateById(message);
        if (b) {
            return ajaxSuccess("200", "审核成功!");
        } else {
            return ajaxSuccess("500", "审核失败!");
        }
    }

    /**
     * 编辑留言
     *
     * @param message
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin","super_admin"},mode = SaMode.OR)
    @SaCheckPermission(value = {"user_update","all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping("/updateMessage")
    public Object updateMessage(@RequestBody Message message) {
        boolean b = messageService.updateById(message);
        if (b) {
            return ajaxSuccess("200", "编辑成功!");
        } else {
            return ajaxSuccess("500", "编辑失败!");
        }
    }

}
